package com.supply.dl.utils;

import android.app.Activity;
import android.widget.EditText;

import com.supply.dl.R;
import com.supply.dl.bean.JobInfoBean;
import com.supply.dl.bean.MeasureUnit;
import com.supply.dl.bean.PositionInfoBean;

import java.math.BigDecimal;
import java.util.ArrayList;

/**
 * 对输入计量 件数，吨数，方数 检查比对处理并且给出结果
 * <p>
 * Author: tian <br>
 * Time: 2018/8/31 15:32
 */
public class DealWithInputUtil {

    /**
     * activity对象
     */
    private Activity ac;

    /**
     * 当前物料对象
     */
    private MeasureUnit currentMaterier;

    /**
     * 当前展示作业信息或者定位信息的item项目
     */
    private MeasureUnit itemData;

    /**
     * 当前展示 作业信息或者定位信息的 列表数据
     */
    private ArrayList<MeasureUnit> itemList;

    public DealWithInputUtil(Activity ac, MeasureUnit currentMaterier, MeasureUnit itemData, ArrayList<MeasureUnit> itemList) {
        this.ac = ac;
        this.currentMaterier = currentMaterier;
        this.itemData = itemData;
        this.itemList = itemList;
    }

    /**
     * 处理领料发货作业的输入
     *
     * @param input
     * @param view
     */
    public void dealMaterierSendOperateInput(BigDecimal input, EditText view) {
        switch (view.getId()) {

            //判断件数
            case R.id.work_info_item_showNumberForPieceTv:
                dealWithOperateAmout(input, view);
                break;

            //吨数
            case R.id.work_info_item_showWeightForTonTv:
                dealWithOperateDun(input, view);
                break;

            //判断方数
            case R.id.work_info_item_showVolumeForSquareTv:
                dealWithOperateVolume(input, view);
                break;
        }
    }

    /**
     * 对作业件数的处理
     */
    private void dealWithOperateAmout(BigDecimal input, EditText view) {
        //物料未主页总量判断
        if (currentMaterier.detailAmount == null || currentMaterier.detailAmount.floatValue() == 0) {
            view.setHint("不以件进行计量，请勿输入");
            view.setText("");
            return;
        }

        BigDecimal totalAmout = new BigDecimal("0");

        if (itemData instanceof PositionInfoBean) {
            //已经填写的物料 未作业量进行求和比对
            for (MeasureUnit item : itemList) {
                if (!item.currentTime.equals(itemData.currentTime) && itemData.amount != null)
                    totalAmout.add(itemData.amount);
            }

        }
        // 对已作业的情况
        else if (itemData instanceof JobInfoBean) {
            //已经填写的物料 未作业量进行求和比对
            for (MeasureUnit item : itemList) {
                if (!item.currentTime.equals(itemData.currentTime) && itemData.amount != null)
                    totalAmout.add(itemData.actualAmount);
            }
        }

        if (input.compareTo(currentMaterier.detailAmount.subtract(totalAmout)) > 0) {
            view.setHint("当前作业总量为" +
                    currentMaterier.detailAmount.subtract(totalAmout).floatValue() + ",输入过大!");
            view.setText("");
            return;
        }

        //有库存
        if (itemData.fromSelectStock) {
            if (itemData.availableInventoryPiece == null || itemData.availableInventoryPiece.floatValue() == 0) {
                view.setHint("可用库存为0件");
                view.setText("");
                return;
            }

            //有库存的时候需要进行比对
            if (input.floatValue() > itemData.availableInventoryPiece.floatValue()) {
                view.setHint("输入大于可用库存:" + itemData.availableInventoryPiece.floatValue() + ",请重新输入");
                view.setText("");
                return;
            }

            itemData.amount = input;
            itemData.actualAmount = itemData.amount;
            return;
        }

        itemData.amount = input;
        itemData.actualAmount = itemData.amount;
    }

    /**
     * 对作业吨数的处理
     */
    private void dealWithOperateDun(BigDecimal input, EditText view) {
        //物料未主页总量判断
        if (currentMaterier.detailWeight == null || currentMaterier.detailWeight.floatValue() == 0) {
            view.setHint("不以吨进行计量，请勿输入");
            view.setText("");
            return;
        }

        BigDecimal totalAmout = new BigDecimal("0");
        if (itemData instanceof PositionInfoBean) {
            //已经填写的物料 未作业量进行求和比对
            for (MeasureUnit item : itemList) {
                if (!item.currentTime.equals(itemData.currentTime) && itemData.weight != null)
                    totalAmout.add(itemData.weight);
            }
        }
        // 对已作业的情况
        else if (itemData instanceof JobInfoBean) {
            //已经填写的物料 未作业量进行求和比对
            for (MeasureUnit item : itemList) {
                if (!item.currentTime.equals(itemData.currentTime) && itemData.actualWeight != null)
                    totalAmout.add(itemData.actualWeight);
            }
        }

        if (input.compareTo(currentMaterier.detailWeight.subtract(totalAmout)) > 0) {
            view.setHint("当前作业总量为" +
                    currentMaterier.detailWeight.subtract(totalAmout).floatValue() + ",输入过大!");
            view.setText("");
            return;
        }

        //有库存
        if (itemData.fromSelectStock) {
            if (itemData.availableInventoryTon == null || itemData.availableInventoryTon.floatValue() == 0) {
                view.setHint("可用库存为0吨");
                view.setText("");
                return;
            }

            //有库存的时候需要进行比对
            if (input.floatValue() > itemData.availableInventoryTon.floatValue()) {
                view.setHint("输入大于可用库存:" + itemData.availableInventoryTon.floatValue() + ",请重新输入");
                view.setText("");
                return;
            }

            itemData.amount = input;
            itemData.actualAmount = itemData.amount;
            return;
        }

        itemData.amount = input;
        itemData.actualAmount = itemData.amount;
    }

    /**
     * 对作业体积数的处理
     */
    private void dealWithOperateVolume(BigDecimal input, EditText view) {
        //物料未主页总量判断
        if (currentMaterier.detailVolume == null || currentMaterier.detailVolume.floatValue() == 0) {
            view.setHint("不以方进行计量，请勿输入");
            view.setText("");
            return;
        }

        BigDecimal totalAmout = new BigDecimal("0");
        if (itemData instanceof PositionInfoBean) {
            //已经填写的物料 未作业量进行求和比对
            for (MeasureUnit item : itemList) {
                if (!item.currentTime.equals(itemData.currentTime) && itemData.volume != null)
                    totalAmout.add(itemData.volume);
            }
        }
        // 对已作业的情况
        else if (itemData instanceof JobInfoBean) {
            //已经填写的物料 未作业量进行求和比对
            for (MeasureUnit item : itemList) {
                if (!item.currentTime.equals(itemData.currentTime) && itemData.actualVolume != null)
                    totalAmout.add(itemData.actualVolume);
            }
        }

        if (input.compareTo(currentMaterier.detailVolume.subtract(totalAmout)) > 0) {
            view.setHint("当前作业总量为" +
                    currentMaterier.detailVolume.subtract(totalAmout).floatValue() + ",输入过大!");
            view.setText("");
            return;
        }

        //有库存
        if (itemData.fromSelectStock) {
            if (itemData.availableInventorySquare == null || itemData.availableInventorySquare.floatValue() == 0) {
                view.setHint("可用库存为0方");
                view.setText("");
                return;
            }

            //有库存的时候需要进行比对
            if (input.floatValue() > itemData.availableInventorySquare.floatValue()) {
                view.setHint("输入大于可用库存:" + itemData.availableInventorySquare.floatValue() + ",请重新输入");
                view.setText("");
                return;
            }

            itemData.amount = input;
            itemData.actualAmount = itemData.amount;
            return;
        }

        itemData.amount = input;
        itemData.actualAmount = itemData.amount;
    }
}
